<script setup lang="ts">
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { nextTick, ref } from "vue";
import { useClassStore } from "../../store";
import { storeToRefs } from "pinia";
import { ApiClass } from "../../api";

const { classRoomList } = storeToRefs(useClassStore());
const {classroomListHandler}=useClassStore()
const show = ref<boolean>(false);
const model = ref<API.Class.Begin>({
  cls_id: 0,
  cls_clsr_id: 0,
});
const emits = defineEmits<{ (name: "save", model: API.Class.Begin): void }>();
const formRef = ref<FormInstance | null>(null);
const cancel = async () => {
  await ElMessageBox.confirm("确定要取消吗？", "提示", { type: "warning" });
  show.value = false;
};
defineExpose({
  begin: async (data: API.Class.Begin) => {
    await ElMessageBox.confirm("确定要开班吗？", "提示", { type: "warning" });
    // formRef.value?.resetFields();   
     nextTick(() => (show.value = true));
    classroomListHandler()
    model.value.cls_id = data.cls_id;
    model.value.cls_clsr_id = 0;


  
  },
});
const save = async () => {
    // formRef.value?.validate()
     await ApiClass.begin(model.value);
     emits("save", model.value); 
      ElMessage.success("开班成功");
    show.value = false;
}
</script>

<template>
  <el-dialog title="开班" width="500px" v-model="show">
    <el-form ref="form" :model="model" label-width="80px">
      <el-form-item label="教室">
        <el-select v-model="model.cls_clsr_id" placeholder="">
          <el-option
            v-for="item in classRoomList?.filter(item=>item.clsr_occupy===0)"
            :key="item.clsr_id"
            :label="item.clsr_name"
            :value="item.clsr_id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<style scoped></style>
